package 二分法;

/**
 * @author zhengTao
 * @version 1.0
 * @description:  二分查找  有效的完全平方数
 * @date 2022/10/14 13:48
 */
public class Demo367 {
    public static void main(String[] args) {
        boolean perfectSquare = isPerfectSquare(5);
        System.out.println(perfectSquare);
    }
    public  static  boolean isPerfectSquare(int num) {
            if(num<=1){
                return num==1;
            }
            int left=0,right=num;
            while (left<=right){
                int mid=left+(right-left)/2;
                if(num/mid>mid){
                    left=mid+1;
                }else if(num/mid<mid){
                    right=mid-1;
                }else {
                    //target=5. 此时mid=2都不满足以上条件，所以会进入这一步，返回时再做一次判断即可
                   return num==mid*mid;
                }
            }
            return false;
    }
}
